using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Security.Cryptography;
using PlugNT.Safe;
using PlugNT.Database.Common;
using PlugNT.Database.Common.Simple;
using PlugNT.Cms.Custom;
using PlugNT.Cms.Model;

namespace PlugNT.Cms.DAL
{



    public class Sys
    {
        private static string currTableName = WebConfig.TablePrefix + "sys";








        public SysInfo GetSysInfo()
        {
            IDataReader dr = DbHelper.ExecuteReader("select top 1 * from " + currTableName);
            SysInfo model = null;
            if (dr != null)
            {
                if (dr.Read())
                {
                    model = new SysInfo();
                    model.id = int.Parse(dr["id"].ToString());
                    model.str_filter = dr["str_filter"].ToString();
                    model.users_admin_html = dr["users_admin_html"].ToString();
                    model.updated_count = int.Parse(dr["updated_count"].ToString());
                    model.updated_time = DateTime.Parse(dr["updated_time"].ToString());
                    model.new_users = dr["new_users"].ToString();
                    model.max_online_count = int.Parse(dr["max_online_count"].ToString());
                    model.max_online_time = DateTime.Parse(dr["max_online_time"].ToString());
                }
                dr.Close();
            }
            return model;
        }











        public bool Update(SysInfo model)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("update " + currTableName + " set ");
            strSql.Append("str_filter='" + StringHelper.SqlFilter(model.str_filter) + "',");
            strSql.Append("users_admin_html='" + StringHelper.SqlFilter(model.users_admin_html) + "',");
            strSql.Append("updated_count=" + model.updated_count.ToString() + ",");
            strSql.Append("updated_time='" + model.updated_time.ToString("yyyy-MM-dd hh:mm:ss") + "',");
            strSql.Append("new_users='" + StringHelper.SqlFilter(model.new_users) + "',");
            strSql.Append("max_online_count=" + model.updated_count.ToString() + ",");
            strSql.Append("max_online_time='" + model.updated_time.ToString("yyyy-MM-dd hh:mm:ss") + "'");
            strSql.Append(" where id=" + model.id.ToString());

            return (DbHelper.ExecuteSql(strSql.ToString()) > 0) ? true : false;
        }



        public bool UpdateStrfilter(int id, string strFilter)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update " + currTableName + " set ");
            strSql.Append("str_filter='" + StringHelper.SqlFilter(strFilter) + "' where id=" + id.ToString());

            return (DbHelper.ExecuteSql(strSql.ToString()) > 0) ? true : false;
        }

        public bool UpdateUsersadminhtml(int id, string usersAdminHtml)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update " + currTableName + " set ");
            strSql.Append("users_admin_html='" + StringHelper.SqlFilter(usersAdminHtml) + "' where id=" + id.ToString());

            return (DbHelper.ExecuteSql(strSql.ToString()) > 0) ? true : false;
        }

        public bool UpdateNewusers(int id, string newUsers)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update " + currTableName + " set ");
            strSql.Append("new_users='" + StringHelper.SqlFilter(newUsers) + "' where id=" + id.ToString());

            return (DbHelper.ExecuteSql(strSql.ToString()) > 0) ? true : false;
        }



        public bool UpdateCountAndTime(int id,int updatedCount, DateTime time)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update " + currTableName + " set ");
            strSql.Append("updated_count=" + updatedCount + ",");
            strSql.Append("updated_time='" + time.ToString("yyyy-MM-dd hh:mm:ss") + "'");
            strSql.Append(" where id=" + id.ToString());

            return (DbHelper.ExecuteSql(strSql.ToString()) > 0) ? true : false;
        }


        public bool UpdateMaxOnline(int id, int maxCount, DateTime time)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update " + currTableName + " set ");
            strSql.Append("max_online_count=" + maxCount + ",");
            strSql.Append("max_online_time='" + time.ToString("yyyy-MM-dd hh:mm:ss") + "'");
            strSql.Append(" where id=" + id.ToString());

            return (DbHelper.ExecuteSql(strSql.ToString()) > 0) ? true : false;
        }





    }
}

